home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17704 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointer to non-static method
  5. Date: Wed, 17 Apr 1996 00:27:08 -0400
  6. Organization: Datalytics, Inc.
  7. Message-ID: <3174731C.4B7B@datalytics.com>
  8. References: <Dpn2MG.9y0@oce.nl> <316E4C6F.3738@Athene.co.uk>
  9. NNTP-Posting-Host: 204.62.224.241
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. C.J. Scaife wrote:
  16. > Emile Heyns wrote:
  17. > >
  18. > > Hi all,
  19. > >
  20. > > I am looking for a way to store and use a pointer to a method. I will
  21. > > be using this pointer from within another method of the same object
  22. > > (I will remain "within" the object). So far, I have been using thus
  23. > > kludge:
  24. > >[snip]
  25. > >
  26. > > but this will limit me to instantiating only one object of this class.
  27. > > Any ideas?
  28. > >[snip]
  29. > Here is something I wrote some years ago. I think it does exactly what
  30. > you want. The cs_state class can be inherited to give this method
  31. > pointer facility to more complex derived classes. Let me know if you
  32. > need help with it :)
  33.  
  34. What he really needs is a pointer to a member function.  The 
  35. syntax is little different from a "normal" function pointer:
  36.  
  37. typedef return_type (class_name::* type_name)(parameter_list);
  38.  
  39. This can be declared within the class declaration.  Now declare 
  40. a dm of that type:
  41.  
  42. type_name m_Method;
  43.  
  44. To invoke the function currently assigned to the pointer, use 
  45. this syntax:
  46.  
  47. (this->*m_Method)(parameters);
  48.  
  49. -- 
  50. Rob Stewart    | My opinions are generally my own.  They do
  51. Datalytics, Inc.| not necessarily reflect those of my employer.
  52.